-- To decode the JSON data, add this file to your project, run
--
--     elm-package install NoRedInk/elm-decode-pipeline
--
-- add these imports
--
--     import Json.Decode exposing (decodeString)`);
--     import RealResult_Module exposing (realResult)
--
-- and you're off to the races with
--
--     decodeString realResult myJsonString

module RealResult_Module exposing
    ( RealResult
    , realResultToString
    , realResult
    , Body
    , Head
    )

import Json.Decode as Jdec
import Json.Decode.Pipeline as Jpipe
import Json.Encode as Jenc
import Dict exposing (Dict, map, toList)
import List exposing (map)

{-| 实时成绩

head:
头部数据

body:
主体数据
-}
type alias RealResult =
    { head : Head
    , body : Body
    }

{-| 主体数据

team:
代表队/代表团/俱乐部

athleteName:
运动员姓名

athleteBib:
运动员号码

athleteTelephone:
运动员联系方式

resultA:
A线成绩

resultB:
B线成绩

rank:
排名
-}
type alias Body =
    { team : String
    , athleteName : String
    , athleteBib : String
    , athleteTelephone : String
    , resultA : String
    , resultB : String
    , rank : String
    }

{-| 头部数据

deviceID:
设备Id

messageUUID:
消息Uuid

timestamp:
发送消息的时间。格式：yyyy-MM-dd HH:mm:ss.fff 比如：2020-09-02 12:24:09.923

messageType:
消息类型
-}
type alias Head =
    { deviceID : String
    , messageUUID : String
    , timestamp : String
    , messageType : String
    }

-- decoders and encoders

realResultToString : RealResult -> String
realResultToString r = Jenc.encode 0 (encodeRealResult r)

realResult : Jdec.Decoder RealResult
realResult =
    Jpipe.decode RealResult
        |> Jpipe.required "Head" head
        |> Jpipe.required "Body" body

encodeRealResult : RealResult -> Jenc.Value
encodeRealResult x =
    Jenc.object
        [ ("Head", encodeHead x.head)
        , ("Body", encodeBody x.body)
        ]

body : Jdec.Decoder Body
body =
    Jpipe.decode Body
        |> Jpipe.required "Team" Jdec.string
        |> Jpipe.required "AthleteName" Jdec.string
        |> Jpipe.required "AthleteBib" Jdec.string
        |> Jpipe.required "AthleteTelephone" Jdec.string
        |> Jpipe.required "ResultA" Jdec.string
        |> Jpipe.required "ResultB" Jdec.string
        |> Jpipe.required "Rank" Jdec.string

encodeBody : Body -> Jenc.Value
encodeBody x =
    Jenc.object
        [ ("Team", Jenc.string x.team)
        , ("AthleteName", Jenc.string x.athleteName)
        , ("AthleteBib", Jenc.string x.athleteBib)
        , ("AthleteTelephone", Jenc.string x.athleteTelephone)
        , ("ResultA", Jenc.string x.resultA)
        , ("ResultB", Jenc.string x.resultB)
        , ("Rank", Jenc.string x.rank)
        ]

head : Jdec.Decoder Head
head =
    Jpipe.decode Head
        |> Jpipe.required "DeviceId" Jdec.string
        |> Jpipe.required "MessageUuid" Jdec.string
        |> Jpipe.required "Timestamp" Jdec.string
        |> Jpipe.required "MessageType" Jdec.string

encodeHead : Head -> Jenc.Value
encodeHead x =
    Jenc.object
        [ ("DeviceId", Jenc.string x.deviceID)
        , ("MessageUuid", Jenc.string x.messageUUID)
        , ("Timestamp", Jenc.string x.timestamp)
        , ("MessageType", Jenc.string x.messageType)
        ]

--- encoder helpers

makeListEncoder : (a -> Jenc.Value) -> List a -> Jenc.Value
makeListEncoder f arr =
    Jenc.list (List.map f arr)

makeDictEncoder : (a -> Jenc.Value) -> Dict String a -> Jenc.Value
makeDictEncoder f dict =
    Jenc.object (toList (Dict.map (\k -> f) dict))

makeNullableEncoder : (a -> Jenc.Value) -> Maybe a -> Jenc.Value
makeNullableEncoder f m =
    case m of
    Just x -> f x
    Nothing -> Jenc.null
